package com.gec.controller;

import com.gec.domain.Leave;
import com.gec.domain.PageBean;
import com.gec.domain.PageParam;
import com.gec.service.abst.LeaveService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/Leave")
public class LeaveController extends BaseController {

    private final LeaveService leaveService;
    private final String Json = "application/json;charset=UTF-8";

    public LeaveController(LeaveService leaveService) {
        this.leaveService = leaveService;
    }

    //-----------------------[Andy请假]-----------------------------
    //映射地址: /showLeaveForm
    @RequestMapping("/showLeaveForm")
    public String showLeaveForm() {
        //内部转发: /WEB-INF/jsp/leave/leaveForm.jsp
        return "leave/leaveForm";
    }

    //映射地址: /Leave/submitLeave [提交请假]
    @RequestMapping(value = "submitLeave",
            produces = "text/json;charset=UTF-8"
    )
    @ResponseBody
    public String submitLeave(Leave leave) {
        printLeave(leave);
        try {
            leaveService.startProcess(leave);
            return sendSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return sendFailed(e);
        }
    }

    void printLeave(Leave leave) {
        System.out.println("+----------------------------+");
        System.out.println("Initiator:" + leave.getInitiator());
        System.out.println("StartDate:" + leave.getStartDate());
        System.out.println("EndDate:" + leave.getEndDate());
        System.out.println("Reason:" + leave.getReason());
        System.out.println("LeaveType:" + leave.getLeaveType());
        System.out.println("Assignee:" + leave.getAssignee());
        System.out.println("Status:" + leave.getStatus());
        System.out.println("+----------------------------+");
    }


    //-------------------[Andy查看我发起的]-------------------------
    @RequestMapping("/myInitiate")
    public String myInitiate() {
        return "/leave/myInitiate";
    }

    @RequestMapping(value = "/jsonMyInitiate", produces = Json)
    @ResponseBody
    public String jsonMyInitiate(
            @RequestParam("initiator") String initiator,
            Integer page, Integer limit) {
        //{1}封装页码参数
        PageParam param = new PageParam();
        param.setPage(page);
        param.setLimit(limit);
        PageBean<Leave> pBean = null;
        try {
            pBean = leaveService.queryMyInitiate(param, initiator);
            return pBean.toJSON();
        } catch (Exception e) {
            e.printStackTrace();
            return pBean.errMsg(e);
        }
    }

    //-------------------[Candy查看待我审批的]----------------------

    @RequestMapping("/waitMyApprove")
    public String waitMyApprove() {
        return "leave/waitMyApprove";
    }

    @RequestMapping(value = "/jsonWaitMyApprove", produces = Json)
    @ResponseBody
    public String jsonWaitMyApprove(
            @RequestParam("assigneeId") String assigneeId, PageParam param) {
        PageBean<Leave> pBean = null;
        try {

            pBean = leaveService.queryWaitMyApprove(assigneeId, param);
            System.out.println("-----------------------------------------------------");
            System.out.println(pBean);
            System.out.println("-----------------------------------------------------");
            return pBean.toJSON();
        } catch (Exception e) {
            e.printStackTrace();
            return pBean.errMsg(e);
        }
    }

    //-------------------[Candy去审批任务]----------------------

    @RequestMapping("/taskDetail")
    public String taskDetail(String taskId, Model model) {
        Leave leave;
        try {
            leave = leaveService.queryMyTaskByTaskId(taskId);
            model.addAttribute("leave", leave);
            return "leave/taskDetail";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/Error/xxxxxxx";
        }
    }

    @RequestMapping(value = "/doApprove",
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String doApprove(@RequestParam Map<String, Object> map) {
        try {
            leaveService.completeMyApprove(map);
            return sendSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return super.sendFailed(e);
        }
    }

    //自己取消申请
    @RequestMapping(value = "/cancelApplication",
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String cancelApplication(String instId) {
        try {
            leaveService.cancelProcess(instId);
            return sendSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }

    //-------------------[Candy查看我已审批的]----------------------
    @RequestMapping("/myApproved")
    public String myApproved() {
        return "leave/myApproved";
    }

    @RequestMapping(value = "jsonMyApproved", produces = Json)
    @ResponseBody
    public String jsonMyApproved(@RequestParam("assigneeId") String assigneeId, PageParam param) {
        PageBean<Leave> pBean;
        try {
            pBean = leaveService.queryHistoryApproved(param, assigneeId);
            return pBean.toJSON();
        } catch (Exception e) {
            e.printStackTrace();
            return sendError(e.getMessage());
        }

    }

}
